package com.crape.topic.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.crape.common.exceptions.DAOException;
import com.crape.common.ibatis.SqlMapClientDaoSupportEx;
import com.crape.common.model.Subject;
import com.crape.topic.dao.ISubjectDAO;

@Service
public class SubjectDAOImpl extends SqlMapClientDaoSupportEx implements ISubjectDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<Subject> listSubjects(Subject subject) throws DAOException {
		if (subject == null) {
			return null;
		}
		return (List<Subject>) this.queryList("listSubjects", subject);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Subject> getSubjects(Subject subject) throws DAOException {
		return (List<Subject>) this.queryList("getSubjects", subject);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Subject> getAllSubjects(Integer startIndex, Integer pageSize) throws DAOException {
		if (startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<Subject>) this.queryList("getAllSubjects", params);
	}

	@Override
	public Long countAllSubjects() throws DAOException {
		return (Long) this.queryObject("countAllSubjects", null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Subject> getHotSubjects() throws DAOException {
		return (List<Subject>) this.queryList("getHotSubjects", null);
	}

	@Override
	public void insertSubject(Subject subject) throws DAOException {
		if (subject == null) {
			return;
		}
		this.insert("insertSubject", subject);
	}

	@Override
	public void updateSubject(Subject subject) throws DAOException {
		if (subject == null) {
			return;
		}
		this.update("updateSubject", subject);
	}

	@Override
	public Long updateSubjectAttentionCount(Long subjectid) throws DAOException {
		if (subjectid == null) {
			throw new IllegalArgumentException(" subjectid is null ");
		}
		Long count = this.countSubjectAttentionBySubjectid(subjectid);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("subjectid", subjectid);
		params.put("count", count);
		this.update("updateSubjectAttentionCount", params);
		return count;
	}

	@Override
	public void deleteSubjectById(Long id) throws DAOException {
		if (id == null) {
			return;
		}
		this.delete("deleteSubjectById", id);
	}

	@Override
	public Long countSubjectAttentionBySubjectid(Long subjectid) throws DAOException {
		if (subjectid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countSubjectAttentionSubjectid", subjectid);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Subject> getTop8SubjectAttentions(Long accountid) throws DAOException {
		if (accountid == null) {
			return null;
		}
		return (List<Subject>) this.queryList("getTop8SubjectAttentions", accountid);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Subject> getSubjectAttentionsByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws DAOException {
		if (accountid == null || startIndex == null || pageSize == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<Subject>) this.queryList("getSubjectAttentionsByAccountid", params);
	}

	@Override
	public Long countSubjectAttentionsByAccountid(Long accountid) throws DAOException {
		if (accountid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countSubjectAttentionsByAccountid", accountid);
	}

}
